package com.gqb.xml;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.apache.commons.digester.Digester;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;

import com.gqb.spring.bean.BeanB;
import com.gqb.spring.tag.Bean;
import com.gqb.spring.tag.Beans;
import com.gqb.spring.tag.Property;
import com.gqb.spring.tag.Scan;

/**
 * 用来解析xml配置文件
 * @author 小泽玛丽罗
 *
 */
public class ResolveXmlDigester {
	protected final Log logger = LogFactory.getLog(getClass());
	public static void main(String[] args) throws IOException, SAXException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
		Digester digester = new Digester();
		digester.setValidating(false);
		digester.addObjectCreate("beans", Beans.class);

		// beans下有多个bean 只需要一个规则匹配就可以
		digester.addObjectCreate("beans/bean", Bean.class);

		// 当标签 bean 的属性<bean id="" name="">于 前面Bean.class中的属性不一样时，用来设置别名
		digester.addSetProperties("beans/bean", "class", "className");
		// 将标签中的属性赋给对象
		digester.addSetProperties("beans/bean");

		digester.addObjectCreate("beans/bean/property", Property.class);
		digester.addSetProperties("beans/bean/property");
		digester.addObjectCreate("beans/scan", Scan.class);
		//设置向父节点添加自己的方法
		digester.addSetNext("beans/bean/property", "addProperty");
		digester.addSetNext("beans/bean", "addBean");
		
		InputStream in = ClassLoader.getSystemResourceAsStream("beans.xml");
		Beans beans = (Beans) digester.parse(in);
		List<Bean> beanList = beans.getBeans();
		for (Bean bean : beanList) {
			System.out
					.println("bean =================================================>");
			System.out.println("    id ==> " + bean.getId());
			List<Property> props = bean.getProps();
			for (Property prop : props) {
				System.out
						.println("    property =================================================>");
				System.out.println("        name ==> " + prop.getName());
				System.out.println("        ref ==> " + prop.getRef());
				System.out.println("        value ==> " + prop.getValue());
			}
		}
		
		for (Bean bean : beanList) {
			//先来初始化BeanB 因为beanB 比较简单，没有依赖
			if("com.gqb.xml.bean.BeanB".equals(bean.getClassName())){
				Class clazz=Class.forName(bean.getClassName());
				Object object=clazz.newInstance();
				List<Property> props=bean.getProps();
				String methodName="set"+bigCaseFirstChar(props.get(0).getName());
				Method m=clazz.getMethod(methodName, String.class);
				m.invoke(object, props.get(0).getValue());
				BeanB beanB=(BeanB) object;
				System.out.println(beanB.getTitle());
				break;
			}
		}
	}
	
	// 将该字符串的首字母大写
    public static String bigCaseFirstChar(String prop) {
		char ch = (char) (prop.charAt(0) - 32);
		return prop.replaceFirst(prop.charAt(0) + "", ch + "");
    }
}
